var cur1 = 0;
var allPage;
select();
function seek(data) {//封装查找方法
    cur1 = data.cur;
    allPage = data.allPage;
    $("#page").text("[" + cur1 + "/" + allPage + "]");
    $("#body").empty();
    $(data.list).each(function (i, item) {
        $("#body").append("<tr>\n" +
            "    <td style=\"height: 50px;line-height: 50px\"><input type=\"checkbox\" name=\"checkbox\" value=\"" + item.uid + "\">\n" +
            "    </td>\n" +
            "    <td style=\"height: 50px;line-height: 50px\">" + item.uid + "\n" +
            "    </td>\n" +
            "    <td style=\"height: 50px;line-height: 50px\"><a href=\"#\">" + item.name + "\n" +
            "    </a></td>\n" +
            "    <td style=\"height: 50px;line-height: 50px\">" + item.department + "\n" +
            "    </td>\n" +
            "    <td style=\"height: 50px;line-height: 50px\">" + item.age + "\n" +
            "    </td>\n" +
            "    <td style=\"height: 50px;line-height: 50px\"\n" +
            "        class=\"am-hide-sm-only\">" + item.sex + "\n" +
            "    </td>\n" +
            "    <td style=\"height: 50px;line-height: 50px\"\n" +
            "        class=\"am-hide-sm-only\">" + item.date + "\n" +
            "    </td>\n" +
            "    <td style=\"height: 50px;line-height: 50px\" class=\"am-hide-sm-only\"><img\n" +
            "            src=\"" + item.npicPath + "\" width=\"50px\"></td>\n" +
            "    <td>\n" +
            "        <div class=\"am-btn-toolbar \" style=\"margin-top: 10px\">\n" +
            "            <div class=\"am-btn-group am-btn-group-xs\">\n" +
            "                <button type='button'   data-am-modal=\"{target: '#doc-modal-1'}\"   \n" +
            "                        class=\"am-btn am-btn-default am-btn-xs am-text-secondary up conctrol\"\n" +
            "                ><span class=\"am-icon-pencil-square-o\"></span> 编辑\n" +
            "                </button> " +
            "                <div class=\"am-modal am-modal-no-btn\" tabindex=\"-1\" id=\"doc-modal-1\">\n" +
            "                   <div class=\"am-modal-dialog\">\n" +
            "                       <div class=\"am-modal-hd\">编辑用户\n" +
            "                           <a href=\"javascript: void(0)\" class=\"am-close am-close-spin\" data-am-modal-close>&times;</a>\n" +
            "                       </div>\n" +
            "                       <div class=\"am-modal-bd\">\n" +
            "                        <form  class=\"am-form\" >\n" +
            "    <fieldset>\n" +
            "        <div class=\"am-form-group\" style=\"width: 400px;margin: 0px auto\">\n" +
            "            <label>头像：</label>\n" +
            "            <input type=\"file\" name=\"file\"/ ><br>\n" +
            "            <label>部门：</label>\n" +
            "            <select data-am-selected=\"{btnSize: 'sm'}\" name=\"department\">\n" +
            "                <option value=\"1\">研发部门</option>\n" +
            "                <option value=\"2\">市场部门</option>\n" +
            "                <option value=\"3\">测试部门</option>\n" +
            "                <option value=\"4\">财务部门</option>\n" +
            "                <option value=\"5\">运维部门</option>\n" +
            "            </select><br>\n" +
            "            <label>是否担任管理员：</label>\n" +
            "            <select data-am-selected=\"{btnSize: 'sm'}\" name=\"administrator\">\n" +
            "                <option value=\"否\">否</option>\n" +
            "                <option value=\"是\">是</option>\n" +
            "            </select><br>\n" +
            "            <input type=\"hidden\" name='uid' value='"+item.uid+"'>\n" +
            "            <input type=\"hidden\" name='npicPath' value='"+item.npicPath+"'>\n" +
            "            <input value='提交' type=\"button\" class=\"am-btn am-btn-primary\" onclick='up(this)'>\n" +
            "        </div>\n" +
            "    </fieldset>\n" +
            "</form>\n" +
            "                       </div>\n" +
            "                   </div>\n" +
            "                </div>\n" +

            "                <button type='button' onclick='del(this)' \n" +
            "                        class=\"am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only conctrol\"\n" +
            "                ><input type='hidden' value='"+item.uid+"'><span id='col' class=\"am-icon-trash-o\"> <a style='color:#CC1414;' \n" +
            "                        >\n" +
            "                删除</a></span>\n" +
            "                </button>" +
            "<div class=\"am-modal am-modal-confirm\" tabindex=\"-1\">\n" +
            "  <div class=\"am-modal-dialog\">\n" +
            "    <div class=\"am-modal-hd\">删除用户</div>\n" +
            "    <div class=\"am-modal-bd\">\n" +
            "      你确定要删除该用户吗?\n" +
            "    </div>\n" +
            "    <div class=\"am-modal-footer\">\n" +
            "      <span class=\"am-modal-btn\" data-am-modal-cancel>取消</span>\n" +
            "      <span class=\"am-modal-btn\" data-am-modal-confirm>确定</span>\n" +
            "    </div>\n" +
            "  </div>\n" +
            "</div>\n" +
            "            </div>\n" +
            "        </div>\n" +
            "    </td>\n" +
            "</tr>")
    });
    $("#ul").empty();
    for (var i = 1; i <= allPage; i++) {
        $("#ul").prepend(" <li class=\"am-dropdown-header\"><a onclick='jump(" + i + ")'>" + i + "</a></li>")
    }
}

function select(cur) {
    $.getJSON("/select", {"cur": cur}, function (data) {
        seek(data);
    })
}

function jump(i) {
    $("#ul").empty();
    cur1 = i;
    select(cur1)
}

function prev() {//上一页
    if (cur1 <= 1) {
        alert("已经是第一页了");
        return false;
    } else {
        cur1 = cur1-1;
        select(cur1)
    }
}

function first() {
    if (cur1 === 1) {
        return false;
    } else {
        select(1)
    }
}

function last() {
    if (cur1 === allPage) {
        return false;
    } else {
        select(allPage)
    }
}


function next() {//下一页
    if (cur1 >= allPage) {
        alert("已经是最后一页了");
        return false;
    } else {
        cur1 = cur1+1;
        select(cur1)
    }
}

function serch() {
    var sel = document.getElementById('sex');
    var sid = sel.selectedIndex;
    var sex = sel[sid].innerHTML;
    var name = document.getElementById("name").value;
    if (sex == "性别") {
        sex = 0;
    } else if (sex == "男") {
        sex = 1;
    } else if (sex == "女") {
        sex = 2;
    }
    $.getJSON("/select2", {"name": name, "sex": sex}, function (data) {
        seek(data);
    })
}

function del(obj) {
    $(obj).next("div").modal({
        relatedElement: this,
        onConfirm: function() {
            var val = $(obj).children("input").val();
            window.location.href="/del?uid="+val;
        },
        onCancel: function() {

        }
    });
}
function up(obj) {
    var formData = new FormData($(obj).parent().parent().parent()[0]);
    $.ajax({
        type : "post",
        data : formData,
        url : '/updata',
        contentType: false, //必须
        processData: false //必须
    });
    $("#doc-modal-1").modal('close');
    serch(cur1);
}
